﻿using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Redis;
using StackExchange.Redis;
using System;
using System.IO;
using System.Text;

namespace RedisCacheProj
{
    /// <summary>
    ///  Microsoft.Extensions.Caching.Redis 使用方式
    ///  https://www.cnblogs.com/xishuai/p/asp-net-core-use-redis.html
    ///  配置地址：https://stackexchange.github.io/StackExchange.Redis/Configuration
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                RedisCache redis = new RedisCache(new RedisCacheOptions()
                {
                    Configuration = "127.0.0.1:6379,password=63796379,defaultDatabase=1",
                    InstanceName = "cache_",
                });

                redis.SetString("username", "xiaoqiang",
                new DistributedCacheEntryOptions()
                {
                    AbsoluteExpiration = DateTime.Now.AddDays(100)
                });

                var username = redis.GetString("username");

                Console.WriteLine("-----------------------------------------------------------------");

                StringWriter sw = new StringWriter();

                var conn = ConnectionMultiplexer.Connect("127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,password=63796379,defaultDatabase=1,allowAdmin=false", sw);

                var constr = sw.GetStringBuilder().ToString();
                Console.WriteLine(constr);

                redis = new RedisCache(new RedisCacheOptions()
                {
                    Configuration= conn.Configuration
                });

                redis.SetString("username1", "xiaoming",
                    new DistributedCacheEntryOptions()
                    {
                        AbsoluteExpiration = DateTime.Now.AddDays(100)
                    });

                var username1 = redis.GetString("username1");


            }
            catch (Exception ex)
            {

            }
        }
    }
}
